package com.team.appinfo.controller;

import com.team.appinfo.entity.AppVersion;
import com.team.appinfo.service.AppVersionService;
import com.team.appinfo.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;


import java.util.Date;
import java.util.List;

@Controller
public class AppVersionController {
    @Autowired
    private AppVersionService appVersionService;

    //显示添加版本的请求
    @RequestMapping("/goAddVersion")
    public String goAddVersion(Long appid, Model model){
        List<AppVersion> list=this.appVersionService.getAllVersionByAppId(appid);
        model.addAttribute("appVersions",list);
        //将appid保存到作用域
        model.addAttribute("id",appid);

        return "developer/appversionadd";
    }

    //添加app的版本信息
    @RequestMapping("/addVersion")
    public String addVersion(
            AppVersion appVersion,
            HttpServletRequest request,
            @RequestParam(value = "a_downloadLink",required = false) CommonsMultipartFile file){
        //1.实现文件上传
        String path=request.getSession().getServletContext().getRealPath("upload/apk");
        String fileName= FileUploadUtil.uploadFile(path,file);

        //2.将版本信息保存数据库
        appVersion.setApklocpath("upload/apk/"+fileName);  //相对路径
        appVersion.setDownloadlink(path+"/"+fileName);  //下载地址
        appVersion.setApkfilename(file.getOriginalFilename());
        //设置修改时间
        appVersion.setModifydate(new Date());
        //设置所属的appid （由表单隐藏域自动填充到实体）????
        this.appVersionService.addVersion(appVersion);

        return "redirect:goAddVersion?appid="+appVersion.getAppid();
    }

}
